﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WaterRevenueMS.WebUI.Common;
using System.ComponentModel.Composition;
using WaterRevenueMS.Core.Service.BasicSet;
using WaterRevenueMS.WebUI.Extension.Filters;
using WaterRevenueMS.Domain.Common.Models;
using WaterRevenueMS.Domain.ViewModels.BasicSet.RuleOfCode;
using WaterRevenueMS.Componnent.Common;
using System.Linq.Expressions;
using WaterRevenueMS.Componnent.Entities.BasicSet;
using WaterRevenueMS.WebUI.Extension.MvcHelper;

namespace WaterRevenueMS.WebUI.Controllers
{
    [Export]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class RuleOfCodeController : AdminController
    {
        [Import]
        private IRuleOfCodeService RuleOfCodeService { get; set; }

        [AdminLayoutAttribute]
        public ActionResult Index()
        {
            var model = new RuleOfCodeModel();
            return View(model);
        }

        [AdminPermission(PermissionCustomMode.Ignore)]
        public ActionResult List(DataTableParameter param)
        {
            int total = RuleOfCodeService.RuleOfCodes.Count(t => t.IsDeleted == false);

            var expr = BuildSearchCriteria();

            var filterResult = RuleOfCodeService.RuleOfCodes.Where(expr).Select(t => new RuleOfCodeModel
            {
                Id = t.Id,
                Bian_MGZ=t.Bian_MGZ,
                Bian_MDX=t.Bian_MDX,
                Yong_HBHCD=t.Yong_HBHCD,
                Qu_YDMCD=t.Qu_YDMCD,
                Biao_CDMCD=t.Biao_CDMCD,
                Zi_ZBHCD=t.Zi_ZBHCD,
                Zi_ZBHZL=t.Zi_ZBHZL,
                Shui_BHCD=t.Shui_BHCD,
                Bian_MGZDY=t.Bian_MGZDY,
                OrderNo=t.OrderNo

            }).OrderBy(t => t.OrderNo).Skip(param.iDisplayStart).Take(param.iDisplayLength).ToList();

            int sortId = param.iDisplayStart + 1;

            var result = from c in filterResult
                         select new[]
                        {
                            sortId++.ToString(), 
                            c.Bian_MGZ,
                            ParseBianMDX(c.Bian_MDX),
                            c.Yong_HBHCD.ToString(),
                            c.Qu_YDMCD.ToString(),
                            c.Biao_CDMCD.ToString(),
                            c.Zi_ZBHCD.ToString(),
                            c.Zi_ZBHZL.ToString(),
                            c.Shui_BHCD.ToString(),
                            ParseBianMGZDY(c.Bian_MDX,c.Bian_MGZDY),
                            c.Id.ToString()
                        };

            return Json(new
            {
                sEcho = param.sEcho,
                iDisplayStart = param.iDisplayStart,
                iTotalRecords = total,
                iTotalDisplayRecords = total,
                aaData = result
            }, JsonRequestBehavior.AllowGet);
        }

        //public ActionResult Create()
        //{
        //    var model = new RuleOfCodeModel();

        //    return PartialView(model);
        //}

        //[HttpPost]
        //[AdminOperateLog]
        //public ActionResult Create(RuleOfCodeModel model)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        OperationResult result = RuleOfCodeService.Insert(model);
        //        if (result.ResultType == OperationResultType.Success)
        //        {
        //            return Json(result);
        //        }
        //        else
        //        {
        //            return PartialView(model);
        //        }
        //    }
        //    else
        //    {
        //        return PartialView(model);
        //    }
        //}

        public ActionResult Edit(int Id)
        {
            var model = new RuleOfCodeModel();
            var entity = RuleOfCodeService.RuleOfCodes.FirstOrDefault(t => t.Id == Id);
            if (null != entity)
            {
               
                model = new RuleOfCodeModel
                {
                    Bian_MGZ = entity.Bian_MGZ,
                    Bian_MDX = entity.Bian_MDX,
                    DefaultValue = entity.DefaultValue,
                    OrderNo = entity.OrderNo,
                    Yong_HBHCD = entity.Yong_HBHCD,
                    Qu_YDMCD = entity.Qu_YDMCD,
                    Biao_CDMCD = entity.Biao_CDMCD,
                    Zi_ZBHCD = entity.Zi_ZBHCD,
                    Zi_ZBHZL = entity.Zi_ZBHZL,
                    Shui_BHCD = entity.Shui_BHCD,
                    Bian_MGZDY = entity.Bian_MGZDY
                };
                 if (model.Bian_MDX == 1)
                {
                  model.RuleItems.Add(new SelectListItem{Text="区域代码+表册代码+(用户号最大值+增量值)",Value="1"});  
                  model.RuleItems.Add(new SelectListItem{Text="表册代码+(用户号最大值+增量值)",Value="2"});
                  model.RuleItems.Add(new SelectListItem { Text = "用户号最大值+增量值", Value = "3" });
                
                }
                 else if (model.Bian_MDX == 2)
                {
                    model.RuleItems.Add(new SelectListItem { Text = "与用户编号相同", Value = "1" });
                    model.RuleItems.Add(new SelectListItem { Text = "用户编号+水表号序号", Value = "2" });
                }
            }
            return PartialView(model);
        }

        [HttpPost]
        public ActionResult Edit(RuleOfCodeModel model)
        {
            if (ModelState.IsValid)
            {
                OperationResult result = RuleOfCodeService.Update(model);
                if (result.ResultType == OperationResultType.Success)
                {
                    return Json(result);
                }
                else
                {
                    return PartialView(model);
                }

            }
            else
            {
                return PartialView(model);
            }
        }


        public ActionResult Delete(int Id)
        {
            OperationResult result = RuleOfCodeService.Delete(Id);
            return Json(result);
        }

        private Expression<Func<RuleOfCode, Boolean>> BuildSearchCriteria()
        {
            DynamicLambda<RuleOfCode> bulider = new DynamicLambda<RuleOfCode>();
            Expression<Func<RuleOfCode, Boolean>> expr = null;
            if (!string.IsNullOrEmpty(Request["Bian_MGZ"]))
            {
                var data = Request["Bian_MGZ"].Trim();
                Expression<Func<RuleOfCode, Boolean>> tmp = t => t.Bian_MGZ.Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }

            Expression<Func<RuleOfCode, Boolean>> tmpSolid = t => t.IsDeleted == false;
            expr = bulider.BuildQueryAnd(expr, tmpSolid);

            return expr;
        }

        private string ParseBianMDX(byte tag)
        {
            string name = "";
            switch (tag)
            {
                case 0:
                    name = "无效规则";
                    break;
                case 1:
                    name = "用户编号编码规则";
                    break;
                case 2:
                    name = "水表编号编码规则";
                    break;
                default:
                    name = "";
                    break;
            }
            return name;
        }

        private string ParseBianMGZDY(int id, byte tag)
        {
            string name = "";
            if (id == 1)
            {
                switch (tag)
                {
                    case 0:
                        name = "无规则,手工输入";
                        break;
                    case 1:
                        name = "区域代码+表册代码+(用户号最大值+增量值)";
                        break;
                    case 2:
                        name = "表册代码+(用户号最大值+增量值)";
                        break;
                    case 3:
                        name = "用户号最大值+增量值";
                        break;
                    default:
                        name = "";
                        break;
                }
            }
            else
            {
                switch (tag)
                {
                    case 0:
                        name = "无规则,手工输入";
                        break;
                    case 1:
                        name = "与用户编号相同";
                        break;
                    case 2:
                        name = "用户编号+水表号序号";
                        break;
                    default:
                        name = "";
                        break;
                }
            }
            return name;
        }


    }
}

